const schedule = require("node-schedule");
const Models = require("../../models");
const { APPLY_STATUS } = require("../../sys_config");

module.exports = function () {
  //测试使用 5秒后执行
  // const date = new Date();
  // date.setSeconds(date.getSeconds() + 5); //设置为当前时间的5秒后
  // const rules = {
  //   hour: date.getHours(),
  //   minute: date.getMinutes(),
  //   second: date.getSeconds(),
  // };

  //每天0点运行
  const rules = {
    hour: 0,
    minute: 0,
    second: 0,
  };

  schedule.scheduleJob(rules, async () => {
    const yesterday = new Date();
    yesterday.setDate(yesterday.getDate() - 1);
    yesterday.setHours(0);
    yesterday.setMinutes(0);
    yesterday.setSeconds(0);
    const applys = await Models.models.applys.core.findAll({
      where: { date: yesterday },
    });
    const id = applys.reduce((res, cur) => {
      if (cur.status === APPLY_STATUS.PENDING) {
        res.push(cur.id);
      }
      return res;
    }, []);
    if (id.length) {
      await Models.models.applys.core.update(
        { status: APPLY_STATUS.TIMEOUT },
        { where: { id } }
      );
    }
  });
};
